#include "CAnimation.h"
 
CAnimation::CAnimation() {
    CurrentFrame    = 0;
    MaxFrames       = 0;
    FrameInc        = 1;
 
    FrameRate       = 100; //Milliseconds
    OldTime         = 0;
 
    Oscillate       = false;
}
 
void CAnimation::OnAnimate() 
{
    Oscillate = false;
	if(CurrentFrame < 0 || CurrentFrame >= MaxFrames) {
		CurrentFrame = 0;
		return;
	}

	if(OldTime + FrameRate > SDL_GetTicks())
	{
        return;
    }
    OldTime = SDL_GetTicks() + FrameRate;
    CurrentFrame += FrameInc;
 
    if(Oscillate)
	{
        if(FrameInc > 0)
		{
            if(CurrentFrame >= MaxFrames -1)
			{
                FrameInc = -FrameInc;
            }
        }
		else
		{
            if(CurrentFrame <= 0)
			{
                FrameInc = -FrameInc;
            }
             }
    }
	else
	{
        if(CurrentFrame >= MaxFrames -1)
		{

                CurrentFrame = 0;
        }
    }
}
 
// getters:
int CAnimation::getFrameRate()
{
    return FrameRate;
}

int CAnimation::getCurrentFrame()
{
	return CurrentFrame;
}

int CAnimation::getMaxFrames()
{
	return MaxFrames;
}

bool CAnimation::getOscillate()
{
	return Oscillate;
}

// setters:
void CAnimation::setFrameRate(int rate)
{
    FrameRate = rate;
}
 
void CAnimation::setCurrentFrame(int frame)
{
	if(frame < 0 || frame >= MaxFrames) {
		CurrentFrame = 0;
		return;
	}
    else {
        CurrentFrame = frame;
    }
}

void CAnimation::setMaxFrames(int nbFrames)
{
    MaxFrames = nbFrames;
}

void CAnimation::setOscillate(bool oscillate)
{
    Oscillate = oscillate;
}
 